<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfWidgetFormSelectDoubleList represents a multiple select displayed as a double list.
 *
 * This widget needs some JavaScript to work. So, you need to include the JavaScripts
 * files returned by the getJavaScripts() method.
 *
 * If you use symfony 1.2, it can be done automatically for you.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfWidgetFormSelectDoubleList.class.php 30760 2010-08-25 11:50:26Z fabien $
 */
class sfWidgetFormSelectDoubleList extends sfWidgetForm {

    protected function configure($options = array(), $attributes = array()) {
        $this->addRequiredOption('choices');
        $this->addOption('class', 'mws-form-item large');
        $this->addOption('class_select', 'double_list_select');
        $this->addOption('associated_first', true);
        $this->addOption('label_unassociated', 'Unassociated');
        $this->addOption('label_associated', 'Associated');
        $associated_first = isset($options['associated_first']) ? $options['associated_first'] : true;
        $this->addOption('template', <<<EOF
<div style="margin-left: 0px" class="%class%">
  <div class="mws-dualbox clearfix">
           <div class="mws-dualbox-col1">
                
                %unassociated%
                </div>
            <div class="mws-dualbox-col2">
                %associate%
                %associateall%
                <div class="clear"></div>
                %unassociateall%
                %unassociate%
            </div>
            <div class="mws-dualbox-col3">
                %associated%
            </div>
      </div>
</div>
EOF
        );
    }
    public function render($name, $value = null, $attributes = array(), $errors = array()) {
        if (is_null($value)) {
            $value = array();
        }

        $choices = $this->getOption('choices');
        if ($choices instanceof sfCallable) {
            $choices = $choices->call();
        }

        $associated = array();
        $unassociated = array();
        foreach ($choices as $key => $option) {
            if (in_array(strval($key), $value)) {
                $associated[$key] = $option;
            } else {
                $unassociated[$key] = $option;
            }
        }
        $size = isset($attributes['size']) ? $attributes['size'] : (isset($this->attributes['size']) ? $this->attributes['size'] : 10);
        $associatedWidget = new sfWidgetFormSelect(array('multiple' => true, 'choices' => $associated), array('size' => $size, 'class' => $this->getOption('class_select') . '-selected'));
        $unassociatedWidget = new sfWidgetFormSelect(array('multiple' => true, 'choices' => $unassociated), array('size' => $size, 'class' => $this->getOption('class_select')));
        return strtr($this->getOption('template'), array(
                    '%class%' => $this->getOption('class'),
                    '%class_select%' => $this->getOption('class_select'),
                    '%id%' => $this->generateId($name),
                    '%label_associated%' => $this->getOption('label_associated'),
                    '%label_unassociated%' => $this->getOption('label_unassociated'),
                    '%associate%' => sprintf('<button onclick="%s" type="button">></button>', 'sfDoubleList.move(\'unassociated_' . $this->generateId($name) . '\', \'' . $this->generateId($name) . '\'); return false;'),
                    '%unassociate%' => sprintf('<button onclick="%s" type="button"><</button>', 'sfDoubleList.move(\'' . $this->generateId($name) . '\', \'unassociated_' . $this->generateId($name) . '\'); return false;'),
                    '%associateall%' => sprintf('<button onclick="%s" type="button">>></button>', 'sfDoubleList.movetouls(\'unassociated_' . $this->generateId($name) . '\', \'' . $this->generateId($name) . '\'); return false;'),
                    '%unassociateall%' => sprintf('<button onclick="%s" type="button"><<</button>', 'sfDoubleList.movetouls(\'' . $this->generateId($name) . '\', \'unassociated_' . $this->generateId($name) . '\'); return false;'),
                    '%associated%' => $associatedWidget->render($name),
                    '%unassociated%' => $unassociatedWidget->render('unassociated_' . $name),
                ));
    }

    /**
     * Gets the JavaScript paths associated with the widget.
     *
     * @return array An array of JavaScript paths
     */
    public function getJavascripts() {
        return array('/js/double_list.js');
    }
    public function __clone() {
        if ($this->getOption('choices') instanceof sfCallable) {
            $callable = $this->getOption('choices')->getCallable();
            if (is_array($callable)) {
                $callable[0] = $this;
                $this->setOption('choices', new sfCallable($callable));
            }
        }
    }

}
